package com.glorysoft.hlgdpda.ui.materialandtool.view

import android.os.Bundle
import com.glorysoft.hlgdpda.BR
import com.glorysoft.hlgdpda.R
import com.glorysoft.hlgdpda.base.BaseActivity
import com.glorysoft.hlgdpda.databinding.ActivityBindLotBinding
import com.glorysoft.hlgdpda.entity.ui.ToastMessageBean
import com.glorysoft.hlgdpda.ui.materialandtool.viewmodel.CarrierBindViewModel
import kotlinx.android.synthetic.main.activity_bind_lot.*
import kotlinx.android.synthetic.main.activity_bind_lot.etLot


/**
 * FileName: CarrierBindLotActivity
 * Author: XJH
 * Date: 2021/7/19 16:50
 * Description:
 * History:
 */
class CarrierBindLotActivity : BaseActivity<CarrierBindViewModel, ActivityBindLotBinding>(){
    var showPlugin : String? = null
    override fun layoutId(): Int {
        return R.layout.activity_bind_lot
    }

    override fun initVariableId(): Int {
        return BR.carrierBindViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        showPlugin = intent.getStringExtra( "showPlugin")
        if(showPlugin ==  "BIND"){
            setTitle("载具绑定批次")
        }else{
            setTitle("载具解绑批次")
        }

        initEdit(etCarrier,object : EditTextListener{
            override fun onTextChangeListener(s: String) {
                if (showPlugin !=  "BIND"){
                    viewModel.searchCarrierInfo(s,etLot)
                }
            }

        })

        setRightListener(  if(showPlugin ==  "BIND"){
            "绑定"
        }else{
            "解绑"
        }){
            if (etCarrier.text.toString().isEmpty() || etLot.text.toString().isEmpty()){
                viewModel.defUI.toastEvent.value = ToastMessageBean("载具号或批次号不能为空",false)
                return@setRightListener
            }

            if (showPlugin == "BIND"){
                viewModel.bindCarrierToLot(etCarrier.text.toString(),etLot.text.toString())
            }else{
                viewModel.unBindCarrierToLot(etCarrier.text.toString(),etLot.text.toString())
            }

        }
    }

    override fun initData() {
        viewModel.defUI.callObserve.observe(this){
            viewModel.defUI.toastEvent.value = ToastMessageBean(if(showPlugin ==  "BIND"){
                "载具绑定成功"
            }else{
                "载具解绑成功"
            },true)
            etCarrier.setText("")
            etLot.setText("")
        }
    }


}